package cloud.xlh.middleware_manage.controller;

import cloud.xlh.middleware_manage.util.SFTPUtil;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/2/27 16:41
 * @ Description:
 */
public class OneKeyStartController {
    public static void main(String[] args) {
        /*String oneKeyStartBatPath = "D:\\development\\environment\\idea\\IdeaProjects\\xlh_osms\\module\\middleware-manage\\src\\main\\resources\\oneKeyStartBat.bat";

        File batFile = new File(oneKeyStartBatPath);
        boolean batFileExist = batFile.exists();
        System.out.println("batFileExist:" + batFileExist);
        if (batFileExist) {
            callCmd(oneKeyStartBatPath);
        }*/
        callRedisCmd();
        callJenkinsCmd();
        //callHertzBeatCmd();
    }

    private static void  callCmd(String locationCmd) {
        StringBuilder sb = new StringBuilder();
        try {
            Process child = Runtime.getRuntime().exec("cmd /k start " + locationCmd);
            System.out.println("------脚本执行中---------");
            InputStream in = child.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line + "\n");
            }
            in.close();
            try {
                child.waitFor();
            } catch (InterruptedException e) {
                System.out.println("------异常---------");
                System.out.println(e);
            }
            System.out.println("sb:" + sb.toString());
            System.out.println("------执行结束---------");
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    private static void  callRedisCmd() {
        String host  = "192.168.234.204";
        String username = "xlh";
        String password = "Qq,.123,.";
        int port = 22;
        int timeout = 2000;
        SFTPUtil sftpUtil = new SFTPUtil(host, username, password, port, timeout);
        StringBuilder cmdStr = new StringBuilder("")
                .append("/home/xlh/middleware/redis/bin/redis-onekeyhandle.sh /home/xlh/middleware/redis/redis-6.2.12/src/redis-server /home/xlh/middleware/redis/redis-6.2.12/redis.conf");
                //.append("/home/xlh/middleware/redis/bin/redis-onekeystop.sh");
        sftpUtil.executeCmd(cmdStr.toString().getBytes());
    }

    private static void  callJenkinsCmd() {
        String host  = "192.168.234.202";
        String username = "xlh";
        String password = "Qq,.123,.";
        int port = 22;
        int timeout = 2000;
        SFTPUtil sftpUtil = new SFTPUtil(host, username, password, port, timeout);
        StringBuilder cmdStr = new StringBuilder("")
                .append("/home/xlh/jenkins/bin/jenkins-onekeystartup.sh /home/xlh/jenkins/war/jenkins.war /home/xlh/jenkins/logs/jenkins.log 9999");
                //.append("/home/xlh/jenkins/bin/jenkins-onekeystop.sh /home/xlh/jenkins/war/jenkins.war");
        sftpUtil.executeCmd(cmdStr.toString().getBytes());
    }

    private static void  callHertzBeatCmd() {
        String host  = "192.168.234.200";
        String username = "xlh";
        String password = "Qq,.123,.";
        int port = 22;
        int timeout = 2000;
        SFTPUtil sftpUtil = new SFTPUtil(host, username, password, port, timeout);
        StringBuilder cmdStr = new StringBuilder("")
                .append("/home/xlh/hertzbeat/hertzbeat/bin/startup.sh");
                //.append("/home/xlh/hertzbeat/hertzbeat/bin/shutdown.sh");
        sftpUtil.executeCmd(cmdStr.toString().getBytes());
    }
}
